package org.example.controller;

import ch.qos.logback.core.util.InvocationGate;
import lombok.extern.slf4j.Slf4j;
import org.example.Entity.Commodity;
import org.example.Entity.Result;
import org.example.service.CommodityService;
import org.example.utils.GsonUtil;
import org.example.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 商品API
 */
@Validated
@RestController
@RequestMapping("/api/commodity")
@Slf4j
public class CommodityController {

    @Autowired
    private CommodityService commodityService;

    @GetMapping("/list")
    public Result<List> list(@RequestBody String paramStr){
        Map<String, Object> paramMap = GsonUtil.jsonToMap(paramStr);
        //1.分页查询商品列表
        List<Commodity> list = commodityService.list((String) paramMap.get("keyword"), (Integer) paramMap.get("page_number"), (Integer) paramMap.get("page_size"));

        //2.返回结果
        return Result.success(list);
    }

    @PostMapping("/{id}")
    public Result<Commodity> info(@PathVariable("id")Integer id){
        log.info("*****The query is :"+id);
        Commodity commodity = commodityService.findCommodityById(id);
        if (commodity.getName() != null&& !"".equals(commodity.getName())){
            log.info("*****The result name of query is :"+commodity.getName());
        }else{
            log.info("*****The result name of query is null");
        }
        return Result.success(commodity);
    }

}
